package com.sajus.web.billing.ejb.data.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "bill")
@NamedQueries({
	@NamedQuery(name = "Bill.findAll", query = "SELECT b FROM Bill b"),
	@NamedQuery(name = "Bill.findByAccount", query = "SELECT b FROM Bill b WHERE b.id = ( SELECT max(d.id) FROM Bill d WHERE d.account = ?) "),
	@NamedQuery(name = "Bill.findCurrentIndex", query ="SELECT r FROM Bill r WHERE r.id= ( SELECT max(d.id) FROM Bill d )" ),
	@NamedQuery(name = "Bill.findAllWithAccount", query ="SELECT r FROM Bill r WHERE r.account = ? " ),
	})
public class Bill extends PersistentObject{

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;
	
	private Date date;
	private Date billingPeriodStart;
	private Date billingPeriodEnd;
	
	private String currency;
	private double totalAmount;
	private double previousPayments;
	private double chargeAdjustments;
	private double amountDue;
	
	@ManyToOne(cascade = CascadeType.ALL)
	@JoinColumn(name="account")
	private Account account;

	@OneToMany( mappedBy="bill", cascade=CascadeType.ALL )
	private List<BillItem> items;
	
	@Override
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getBillingPeriodStart() {
		return billingPeriodStart;
	}

	public void setBillingPeriodStart(Date billingPeriodStart) {
		this.billingPeriodStart = billingPeriodStart;
	}

	public Date getBillingPeriodEnd() {
		return billingPeriodEnd;
	}

	public void setBillingPeriodEnd(Date billingPeriodEnd) {
		this.billingPeriodEnd = billingPeriodEnd;
	}

	public String getCurrency() {
		return currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	public double getPreviousPayments() {
		return previousPayments;
	}

	public void setPreviousPayments(double previousPayments) {
		this.previousPayments = previousPayments;
	}

	public double getChargeAdjustments() {
		return chargeAdjustments;
	}

	public void setChargeAdjustments(double chargeAdjustments) {
		this.chargeAdjustments = chargeAdjustments;
	}

	public double getAmountDue() {
		return amountDue;
	}

	public void setAmountDue(double amountDue) {
		this.amountDue = amountDue;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public List<BillItem> getItems() {
		return items;
	}

	public void setItems(List<BillItem> items) {
		this.items = items;
	}
	
}
